package com.secondhand.project.service.impl;

import com.secondhand.common.utils.SecurityUtils;
import com.secondhand.project.domain.Likes;
import com.secondhand.project.mapper.LikesMapper;
import com.secondhand.project.service.ILikesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class LikesServiceImpl implements ILikesService {
    @Autowired
    private LikesMapper likesMapper;

    @Override
    public Likes selectLikesById(Long id) {
        return likesMapper.selectLikesById(id);
    }

    @Override
    public List<Likes> selectLikesList(Likes likes) {
        return likesMapper.selectLikesList(likes);
    }

    @Override
    public int insertLikes(Likes likes) {
        likes.setUserId(SecurityUtils.getUserId());
        List<Likes> dbLikesList = likesMapper.selectLikesList(likes);
        if (!dbLikesList.isEmpty()) {//已收藏
            return likesMapper.deleteLikesById(dbLikesList.get(0).getId());
        } else {
            return likesMapper.insertLikes(likes);
        }
    }

    @Override
    public int updateLikes(Likes likes) {
        return likesMapper.updateLikes(likes);
    }

    @Override
    public int deleteLikesByIds(Long[] ids) {
        return likesMapper.deleteLikesByIds(ids);
    }

    @Override
    public int deleteLikesById(Long id) {
        return likesMapper.deleteLikesById(id);
    }
}
